﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.CoreType.Enums;
using System.Collections;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Framework.Utility;
using Sector7.Framework.Enums;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// TableSummaryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TableSummaryWindow : UserControl
    {
        private List<RS_Room> _rooms = new List<RS_Room>();

        public TableSummaryWindow()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            _rooms = Aide.LocalClient.Invoke<List<RS_Room>>(
                service => service.GetAllRooms(true));

            ClientUtil.LoadItemsSource(dtgRoom, _rooms);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void dtgRoom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_Room selectedRoom = dtgRoom.SelectedItem as RS_Room;
            if (selectedRoom != null)
            {
                ClientUtil.LoadItemsSource(dtgTable, selectedRoom.Tables);
            }
        }

        private void btnAddRoom_Click(object sender, RoutedEventArgs e)
        {
            AddRoomWindow addRoomWindow = new AddRoomWindow();
            addRoomWindow.InitializeWindow(CrudType.Create);
            addRoomWindow.AddSuccess += new EventHandler(AddRoomWindow_AddSuccess);

            addRoomWindow.ShowDialog();
        }

        private void AddRoomWindow_AddSuccess(object sender, EventArgs e)
        {
            AddRoomWindow addRoomWindow = sender as AddRoomWindow;
            RS_Room newRoom = addRoomWindow.Room;
            _rooms.Add(newRoom);

            ClientUtil.LoadSelectedItem(dtgRoom, newRoom);
            addRoomWindow.ResetWindow();
        }

        private void btnModifyRoom_Click(object sender, RoutedEventArgs e)
        {
            RS_Room selectedRoom = dtgRoom.SelectedItem as RS_Room;

            if (selectedRoom != null)
            {
                AddRoomWindow modifyRoomWindow = new AddRoomWindow();
                modifyRoomWindow.Room = selectedRoom;
                modifyRoomWindow.InitializeWindow(CrudType.Update);
                modifyRoomWindow.AddSuccess += new EventHandler(modifyRoomWindow_AddSuccess);

                modifyRoomWindow.ShowDialog();
            }
        }

        void modifyRoomWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyRoomWindow = sender as AddRoomWindow;
            RS_Room newRoom = modifyRoomWindow.Room;
            _rooms.Find(delegate(RS_Room room)
            {
                if (room.RoomId == newRoom.RoomId)
                {
                    room = newRoom;

                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgRoom, newRoom);
        }

        private void btnDeleteRoom_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddTable_Click(object sender, RoutedEventArgs e)
        {
            RS_Room selectedRoom = dtgRoom.SelectedItem as RS_Room;

            if (selectedRoom != null)
            {
                AddTableWindow addTableWindow = new AddTableWindow(selectedRoom, _rooms);
                addTableWindow.InitializeWindow(CrudType.Create);
                addTableWindow.AddSuccess += new EventHandler(AddTableWindow_AddSuccess);

                addTableWindow.ShowDialog();
            }
        }

        private void AddTableWindow_AddSuccess(object sender, EventArgs e)
        {
            RS_Room selectedRoom = dtgRoom.SelectedItem as RS_Room;
            AddTableWindow addTableWindow = sender as AddTableWindow;
            RS_Table newTable = addTableWindow.Table;
            _rooms.Find(delegate(RS_Room room)
            {
                if (room.RoomId == selectedRoom.RoomId)
                {
                    room.Tables.Add(newTable);
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgTable, newTable);
            addTableWindow.ResetWindow();
        }

        private void btnModifyTable_Click(object sender, RoutedEventArgs e)
        {
            RS_Room selectedRoom = dtgRoom.SelectedItem as RS_Room;
            RS_Table selectedTable = dtgTable.SelectedItem as RS_Table;

            if (selectedRoom != null && selectedTable != null)
            {
                AddTableWindow modifyTableWindow = new AddTableWindow(selectedRoom, _rooms);
                modifyTableWindow.Table = selectedTable;
                modifyTableWindow.InitializeWindow(CrudType.Update);
                modifyTableWindow.AddSuccess += new EventHandler(modifyTableWindow_AddSuccess);

                modifyTableWindow.ShowDialog();
            }
        }

        void modifyTableWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyTableWindow = sender as AddTableWindow;
            RS_Table newTable = modifyTableWindow.Table;
            _rooms.Find(delegate(RS_Room room)
            {
                if (room.RoomId == newTable.RoomId)
                {
                    room.Tables.Find(delegate(RS_Table table)
                    {
                        if (table.TableId == newTable.TableId)
                        {
                            table = newTable;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });

                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgTable, newTable);
        }

        private void btnDeleteTable_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dtgRoom_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid dtg = sender as DataGrid;
            if (dtg.HasItems)
            {
                dtg.SelectedIndex = 0;
            }
        }
    }
}
